var table = layui.table;
var param = '';
if (getUrlParam('userid') && getUrlParam('preview')) param += '?userid='+getUrlParam('userid')+'&preview=true';
layui.use('table', function () {
	var inst = table.render({
		elem: '#table',
		url: '/profile/logs'+param,
		method: 'get',
		page: true,
		limit: 20,
		limits: [10, 20, 30],
		cols: [[
			{ field: 'course_name', title: '学习内容', width: '274', templet: function (t) {
				return '<i class="title-icon iconfont icon-'+t.icon+'"></i>' + t.course_name;
			}},
			{ field: 'date', title: '日期', align: 'center', width: '150' },
			{ field: 'grade', title: '等级', align: 'center', width: '120' },
			{
				field: 'change', title: '变化', align: 'center', width: '60', templet: function (d) {
					if (d.change === 1) {
						return '<span style="color:#00a60e"><i class="iconfont icon-jiantou_top"></i></span>';
					} else if(d.change === 0) {
						return '<span style="color:#d92916"><i class="iconfont icon-jiantou_down"></i></span>';
					} else {
						return '-'
					}
				}
			},

			{ field: 'problem', title: '正确数/全部问题数', align: 'center', width: '140' },
			{ field: 'time', title: '时间', align: 'right', width: '90' }
		]],
		parseData: function (res) {
			$('.count h2').eq(0).text(res.nums[3] ?? 0);
			$('.count h2').eq(1).text(res.nums[2] ?? 0);
			$('.count h2').eq(2).text(res.nums[1] ?? 0);
			return {
				"code": 0,
				"msg": '',
				"count": res.count,
				"data": res.data
			};
		},
		request: {
			pageName: 'page',
			limitName: 'size'
		},
	});
});

layui.use('form', function(){
	var form = layui.form;
	form.on('select(time)', function(data){
		reloads()
	});
	form.on('select(type)', function(data){
		reloads()
	});
});
function reloads(){
	table.reload('table', {
		where: {
			time: $('.time').val(),
			type: $('.type').val(),
		},
		page: {
			curr: 1
		}
	});
}
function getUrlParam(name) {   
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = top.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]); return null;
}